﻿using GalaSoft.MvvmLight.Command;
using Infralution.Localization.Wpf;
using MuddyBoots.GreenlightQC.Mobile.UI.ViewModel;
using MuddyBoots.GreenlightQC.Mobile.UI.Helpers;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MuddyBoots.GreenlightQC.Mobile.UI.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();

            this.Loaded += (sender, args) =>
            {
                CultureManager.UICulture = Thread.CurrentThread.CurrentCulture;
                CultureManager.UICultureChanged += this.HandleUICultureChanged;

                this.UpdateLanguageMenus();

                // Standard key bindings...
                var standardBindings = new[]
                {
                    new KeyBinding(new RelayCommand(this.HandleEnglish), Key.F1, ModifierKeys.None),
                    new KeyBinding(new RelayCommand(this.HandleFrench), Key.F2, ModifierKeys.None),
                    new KeyBinding(new RelayCommand(this.HandleGerman), Key.F3, ModifierKeys.None)
                };

                this.InputBindings.AddRange(standardBindings);
            };

            this.Closing += (sender, args) =>
            {
                CultureManager.UICultureChanged -= this.HandleUICultureChanged;
                ViewModelLocator.Cleanup();
            };
        }

        void HandleEnglish()
        {
            if (!this.languageMenuEnglish.IsEnabled)
            {
                return;
            }

            this.HandleEnglish(this, new RoutedEventArgs());
        }

        void HandleFrench()
        {
            if (!languageMenuFrench.IsEnabled)
            {
                return;
            }

            HandleFrench(this, new RoutedEventArgs());
        }

        void HandleGerman()
        {
            if (!languageMenuGerman.IsEnabled)
            {
                return;
            }

            HandleGerman(this, new RoutedEventArgs());
        }

        void HandleGerman(object sender, RoutedEventArgs args)
        {
            if (CultureManager.UICulture.Name == "de-DE")
            {
                return;
            }

            var culture = new CultureInfo("de-DE");
            CultureManager.UICulture = culture;
        }

        private void HandleFrench(object sender, RoutedEventArgs e)
        {
            if (CultureManager.UICulture.Name == "fr-FR")
            {
                return;
            }

            var culture = new CultureInfo("fr-FR");
            CultureManager.UICulture = culture;
        }

        private void HandleEnglish(object sender, RoutedEventArgs e)
        {
            if (CultureManager.UICulture.Name == "en-GB")
            {
                return;
            }

            var culture = new CultureInfo("en-GB");
            CultureManager.UICulture = culture;
        }

        void HandleExit(object sender, RoutedEventArgs args)
        {
            this.Close();
        }

        void HandleUICultureChanged(object sender, EventArgs args)
        {
            this.UpdateLanguageMenus();
        }

        void UpdateLanguageMenus()
        {
            var lang = CultureManager.UICulture.TwoLetterISOLanguageName.ToLower();
            languageMenuFrench.IsChecked = (lang == "fr");
            languageMenuFrench.IsEnabled = (lang != "fr");
            languageMenuEnglish.IsChecked = (lang == "en");
            languageMenuEnglish.IsEnabled = (lang != "en");
            languageMenuGerman.IsChecked = (lang == "de");
            languageMenuGerman.IsEnabled = (lang != "de");
        }

        private void OnNavigationClick(object sender, RoutedEventArgs e)
        {
            var activeButton = sender as ToggleButton;

            var buttons = this.FindLogicalChildren<ToggleButton>().Where(x => x.Tag.Equals("Navigation")) ;
            foreach (ToggleButton button in buttons)
            {
                button.IsChecked = button.Equals(activeButton);
            }
        }
    }
}
